package arithmetic;

/**
 * @program: javaLearn
 * @description: 搜索插入值 二分
 * @author: renhai
 * @Date ：2022/3/10 22:46
 **/
public class searchInsert {
    public int searchInsert(int[] nums, int target) {
        int left = 0;//左指针
        int right = nums.length - 1;//右指针
        int mid;//
        while (left <= right) {
            mid = left + ((right - left) >> 1);
            if (nums[mid] < target) {
                left = mid + 1;
            } else if (nums[mid] > target) {
                right = mid - 1;
            } else {
                return mid;
            }
        }
        return right+1;
    }

}
